package org.csu.sei.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.csu.sei.common.BaseConstant;
import org.csu.sei.common.ImageUpload;
import org.csu.sei.common.ReturnJson;
import org.csu.sei.entity.Banner;
import org.csu.sei.entity.DTO.BannerDTO;
import org.csu.sei.service.IBannerService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.csu.sei.common.BaseConstant.ErrorCode.*;
import static org.csu.sei.common.BaseConstant.Status.*;

@RestController
@RequestMapping("/banner")
@ResponseBody
public class BannerController {

    @Resource
    private IBannerService bannerService;

    @GetMapping("/all")
    @ApiOperation(value = "查询所有Banner")
    public JSONObject queryAllBanner(@RequestHeader(value = "token" ,required = false)String token){
        JSONObject data = new JSONObject();
        List<Banner> banners;
        if(token==null){
            banners = bannerService.queryAll(false);
            List<BannerDTO> bannerDTOS = new ArrayList<>();
            for (Banner banner:banners) {
                bannerDTOS.add(BannerDTO.returnBanner(banner));
                System.out.println(BannerDTO.returnBanner(banner).toString());
            }
            data.put("banners",bannerDTOS);
        }
        else{
            banners = bannerService.queryAll(true);
            data.put("banners",banners);
        }

        return ReturnJson.successRes(data);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询Banner")
    public JSONObject queryBanner(@PathVariable Long id){
        JSONObject data = new JSONObject();
        Banner banner=bannerService.queryBanner(id, ALL);
        if(banner==null){
            return ReturnJson.defaultFailureRes(BANNER_NOT_EXIST);
        }
        banner.setId(null);
        BannerDTO bannerDTO = BannerDTO.returnBanner(banner);
        data.put("banner",bannerDTO);
        return ReturnJson.successRes(JSON.parseObject(data.toString()));
    }


    @PostMapping("")
    @ApiOperation(value = "新建并插入轮播图")
    public JSONObject insertBanner(@RequestBody Banner banner) {
        banner.setBannerOrder(bannerService.lastestOrder()+1);
        bannerService.insertBanner(banner);
        return ReturnJson.ackWithMsg("ok");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除相应的轮播图")
    public JSONObject deleteBanner(@PathVariable Long id){
        int result=bannerService.deleteBanner(id);
        if(result==-1){
            return ReturnJson.defaultFailureRes(BANNER_NOT_EXIST);
        }
        return ReturnJson.ackWithMsg("delete succeed");
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "修改轮播图内容" )
    public JSONObject updateBanner(@PathVariable Long id, @RequestBody Banner banner){
        banner.setId(id);
        int result=bannerService.updateBanner(banner);
        if(result==-1){
            return ReturnJson.defaultFailureRes(BANNER_NOT_EXIST);
        }
        return ReturnJson.ackWithMsg("ok");
    }

    @PutMapping("/order")
    @ApiOperation(value = "设置轮播顺序")
    public JSONObject changeBannerOrder(@RequestBody List<Long> order){
        int result = bannerService.changeOrder(order);
        if (result==-1){
            return ReturnJson.defaultFailureRes(BANNER_NOT_EXIST);
        }
        return ReturnJson.ackWithMsg("ok");

    }

    //设置不可见，有设置可见，
    @PutMapping("/{id}/invisible")
    @ApiOperation(value = "设置轮播图可见状态接口",notes = "设置可见不可见，每次请求会将其变为相反的状态")
    public JSONObject setInvisible(@PathVariable Long id){
        int result=bannerService.changeStatus(id);
        if(result==-1){
            return ReturnJson.defaultFailureRes(BANNER_NOT_EXIST);
        }
        return ReturnJson.ackWithMsg(String.valueOf(result));
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传图片至OSS服务器并返回图片的链接地址")
    public JSONObject uploadImage(@RequestParam("image_upload")MultipartFile imageUpload){
        JSONObject json = new JSONObject();
        String url="";
        try{
            url= ImageUpload.getUrl(BaseConstant.OSSConfig.BANNER_DIR,imageUpload);
            json.put("image_url",url);
            return ReturnJson.successRes(json);
        }
        catch (Exception e){
            return ReturnJson.defaultFailureRes(IMAGE_UPLOAD_OSS_ERROR);
        }
    }

    @PutMapping("/{id}/relevance")
    public JSONObject relateNews(@PathVariable Long id,@RequestBody Map<String,Long>map ){
        Long newsId=map.get("news_id");
        int result = bannerService.relateNews(id,newsId);
        if(result==-1){
            return ReturnJson.defaultFailureRes(NEWS_NOT_EXIST);
        }
        return ReturnJson.ackWithMsg("ok");
    }
}
